package com.fangs.kotlin_banner

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fangs.kotlin_banner.adapter.PointQuickAdapter
import com.fangs.kotlin_banner.banner.Banner
import com.fangs.kotlin_banner.banner.BannerPage
import com.fangs.kotlin_banner.utils.APPUtils
import java.util.ArrayList

class MainActivity : AppCompatActivity() {

    private var homeBanner: Banner? = null
    private var rlPoint: RecyclerView? = null
    private var pointQuickAdapter: PointQuickAdapter? = null
    private val bannerList: ArrayList<BannerPage> = ArrayList<BannerPage>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        rlPoint = findViewById(R.id.rl_point)
        homeBanner = findViewById(R.id.home_banner)


        bannerList.add(
            BannerPage(
                "https://img.ltwebstatic.com/images3_ach/2022/06/10/1654861945d4e2fdf787b3001b704e1460588ae75e.webp",
                2000
            )
        )
        bannerList.add(
            BannerPage(
                "https://img.ltwebstatic.com/images3_ach/2022/06/10/1654861945d4e2fdf787b3001b704e1460588ae75e.webp",
                2000
            )
        )
        bannerList.add(
            BannerPage(
                "https://gossv.cfp.cn/videos/mts_videos/medium/VCG2216830485.mp4",
                "https://img.ltwebstatic.com/images3_ach/2022/06/10/1654861945d4e2fdf787b3001b704e1460588ae75e.webp"
            )
        )

        pointQuickAdapter = PointQuickAdapter()

        homeBanner?.setDataPlay(bannerList)
        rlPoint?.scheduleLayoutAnimation()


        //如果只有一张图或者一个视频，point则隐藏
        if (bannerList.size == 1) {
            rlPoint?.setVisibility(View.GONE)
        }

        pointQuickAdapter?.setNewData(bannerList as List<BannerPage?>?)
        rlPoint?.setNestedScrollingEnabled(true)
        rlPoint?.setLayoutManager(LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false))
        rlPoint?.setAdapter(pointQuickAdapter)
        //点击事件
        homeBanner?.setItemClickListener(object : Banner.OnItemClickListener {
            override fun onItemClick(position: Int) {
                Toast.makeText(this@MainActivity, "点击了" + position, Toast.LENGTH_SHORT).show()
            }
        })

        homeBanner?.setPageChangeListener(object : Banner.OnPageChangeListener {
            override fun onPageSelected(position: Int) {
                if (pointQuickAdapter?.getData()?.isEmpty()!!) {
                    return
                }
                pointQuickAdapter?.setSelectedPosition(position)
                pointQuickAdapter?.notifyDataSetChanged()
            }
        })
    }
}